package com.cpi.cornpp.creditlinebalance.converter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;

import com.cpi.cornpp.creditline.view.CreditLineView;
import com.cpi.cornpp.creditlinebalance.dto.CreditLineBalanceDTO;
import com.cpi.cornpp.creditlinebalance.view.CreditLineBalanceView;
import com.cs.common.utils.StringUtils;

public class CreditLineBalanceConverter {

	/**
	 * converter DTO to View
	 * @param creditLineBalanceDTO
	 * @return
	 */
	public CreditLineBalanceView convert(CreditLineBalanceDTO dto){
		CreditLineBalanceView view = null;
		CreditLineView clView = null;
		if (dto != null){
			view = new CreditLineBalanceView();			
			view.setBalance(dto.getBalance());
			view.setMonthlyPeriodId(dto.getMonthlyPeriodId());
			view.setBalanceEmpty(dto.isBalanceEmpty());
			view.setLabel(new StringBuffer(dto.getShortName()).append(StringUtils.SPACE_CHAR).append(dto.getYear()).toString());
			clView = new CreditLineView();
			clView.setCreditLineId(dto.getCreditLineId());			
			view.setCreditLine(clView);
		}
		return view;
	}
	/**
	 * Convert View to DTO
	 * @param creditLineBalanceView
	 * @return
	 */
	public CreditLineBalanceDTO convert(CreditLineBalanceView view){
		CreditLineBalanceDTO dto = null;
		
		if (view != null){
			dto = new CreditLineBalanceDTO();			
			dto.setBalance(view.getNewBalance());			
			dto.setCreditLineId(view.getCreditLine().getCreditLineId());
			dto.setMonthlyPeriodId(view.getMonthlyPeriodId());
			
		}
		return dto;
	}	
	/**
	 * Convert the View to DTO
	 * @param creditLineBalanceDTO
	 * @return
	 */
	public List convertDTOs(Collection creditLineBalancesDTO){
		List resultList = null;
		resultList = new ArrayList(0);
		Iterator iterator = creditLineBalancesDTO.iterator();
		CreditLineBalanceDTO dto = null;
		if (!CollectionUtils.isEmpty(creditLineBalancesDTO)){
			while( iterator.hasNext() ){
				dto = (CreditLineBalanceDTO)iterator.next();
				resultList.add(convert(dto));
			}
		}
		
		return resultList;
	}
	

	public final List convertViews(Collection creditLineBalancesViews){
		List periods = null;
		
		periods = new ArrayList(0);
		
		if (!CollectionUtils.isEmpty(creditLineBalancesViews)){
			Iterator iterator = creditLineBalancesViews.iterator();
			CreditLineBalanceView view = null;
			while(iterator.hasNext()){
				view = (CreditLineBalanceView)iterator.next();
				periods.add(convert(view));
			}
		}
		
		return periods;
	}
}
